import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-11-13
 * Time: 10:43
 */

//数青蛙：
public class Demo {

    //K,V绑定在一起
    public int minNumberOfFrogs(String c) {
        char[] croakOfFrogs = c.toCharArray();
        String t = "croak";
        int n = t.length();
        int[] hash = new int[n]; // 数组模拟哈希表


        //再new一个哈希表来绑定，某个元素和下标关系
        Map<Character,Integer> index = new HashMap<>();
        for(int i = 0; i < n; i++)
            index.put(t.charAt(i),i);


        for(char ch : croakOfFrogs){
            if(ch == t.charAt(0)){
                if(hash[n-1] != 0) hash[n-1]--;
                hash[0]++;
            }else{
                int i = index.get(ch);
                if(hash[i-1] == 0) return -1;
                hash[i-1]--; hash[i]++;
            }
        }

        for(int i = 0; i <= n-2; i++)
            if(hash[i] != 0)
                return -1;

        return hash[n-1];
    }
}
